package com.example.employ.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.employ.dao.entity.CompaniesDO;
import com.example.employ.dao.entity.JobDO;
import com.example.employ.dao.mapper.JobMapper;
import com.example.employ.dto.response.JobDetailRespDTO;
import com.example.employ.dto.response.JobListRespDTO;
import com.example.employ.dto.response.JobSearchRespDTO;
import com.example.employ.service.CompaniesService;
import com.example.employ.service.JobService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@RequiredArgsConstructor
public class JobServiceImpl extends ServiceImpl<JobMapper, JobDO> implements JobService {
    private final CompaniesService companiesService;

    /**
     * 查看公司招聘的职位(点击公司展现的列表)
     */
    @Override
    public List<JobListRespDTO> jobList(Long companiesId) {
        List<JobDO> jobDOS = null;
        List<JobListRespDTO> result = new ArrayList<>();
        if (companiesId == null){
            LambdaQueryWrapper<JobDO> queryWrapper = Wrappers.lambdaQuery(JobDO.class)
                    .eq(JobDO::getDelFlag, 0);
            jobDOS = baseMapper.selectList(queryWrapper);
            for (JobDO jobDO : jobDOS) {
                CompaniesDO companiesDO = companiesService.getById(jobDO.getCompaniesId());
                JobListRespDTO jobListRespDTO = new JobListRespDTO();
                jobListRespDTO.setAccount(jobDO.getAccount());
                jobListRespDTO.setName(jobDO.getName());
                jobListRespDTO.setId(jobDO.getId());
                jobListRespDTO.setSalary(jobDO.getSalary());
                jobListRespDTO.setCompaniesName(companiesDO.getName());
                result.add(jobListRespDTO);
            }
            return result;
        }
        // 根据公司 ID 获取该公司招聘的职位，并且没有删除
        LambdaQueryWrapper<JobDO> queryWrapper = Wrappers.lambdaQuery(JobDO.class)
                .eq(JobDO::getCompaniesId, companiesId)
                .eq(JobDO::getDelFlag, 0);
        jobDOS = baseMapper.selectList(queryWrapper);
        for (JobDO jobDO : jobDOS) {
            CompaniesDO companiesDO = companiesService.getById(jobDO.getCompaniesId());
            JobListRespDTO jobListRespDTO = new JobListRespDTO();
            jobListRespDTO.setAccount(jobDO.getAccount());
            jobListRespDTO.setName(jobDO.getName());
            jobListRespDTO.setId(jobDO.getId());
            jobListRespDTO.setSalary(jobDO.getSalary());
            jobListRespDTO.setCompaniesName(companiesDO.getName());
            result.add(jobListRespDTO);
        }
        return result;
    }

    /**
     * 职位详情
     * @param companiesId 公司 ID
     * @param jobId   职位 ID
     * @return
     */
    @Override
    public JobDetailRespDTO jobDetail(Long companiesId, Long jobId) {
        // 根据公司 ID 和 职位 ID 获取该公司招聘的职位详情，并且没有删除
        LambdaQueryWrapper<JobDO> queryWrapper = Wrappers.lambdaQuery(JobDO.class)
                .eq(JobDO::getCompaniesId, companiesId)
                .eq(JobDO::getId, jobId)
                .eq(JobDO::getDelFlag, 0);
        JobDO jobDO = baseMapper.selectOne(queryWrapper);
        return BeanUtil.toBean(jobDO, JobDetailRespDTO.class);
    }

    /**
     * 搜索职位(根据岗位名字进行模糊匹配查找)
     * @param name 岗位名字
     * @return
     */
    @Override
    public List<JobSearchRespDTO> search(String name) {
        List<JobDO> jobDOS = null;
        List<JobSearchRespDTO> result = new ArrayList<>();
        if (name == null){
            LambdaQueryWrapper<JobDO> queryWrapper = Wrappers.lambdaQuery(JobDO.class)
                    .eq(JobDO::getDelFlag, 0);
            jobDOS = baseMapper.selectList(queryWrapper);
            for (JobDO jobDO : jobDOS) {
                CompaniesDO companiesDO = companiesService.getById(jobDO.getCompaniesId());
                JobSearchRespDTO jobSearchRespDTO = new JobSearchRespDTO();
                jobSearchRespDTO.setAccount(jobDO.getAccount());
                jobSearchRespDTO.setName(jobDO.getName());
                jobSearchRespDTO.setSalary(jobDO.getSalary());
                jobSearchRespDTO.setId(jobDO.getId());
                jobSearchRespDTO.setCompaniesName(companiesDO.getName());
                result.add(jobSearchRespDTO);
                return result;
            }
        }
        LambdaQueryWrapper<JobDO> queryWrapper = Wrappers.lambdaQuery(JobDO.class)
                .eq(JobDO::getDelFlag, 0)
                .like(JobDO::getName, name);
        jobDOS = baseMapper.selectList(queryWrapper);
        for (JobDO jobDO : jobDOS) {
            CompaniesDO companiesDO = companiesService.getById(jobDO.getCompaniesId());
            JobSearchRespDTO jobSearchRespDTO = new JobSearchRespDTO();
            jobSearchRespDTO.setAccount(jobDO.getAccount());
            jobSearchRespDTO.setName(jobDO.getName());
            jobSearchRespDTO.setSalary(jobDO.getSalary());
            jobSearchRespDTO.setId(jobDO.getId());
            jobSearchRespDTO.setCompaniesName(companiesDO.getName());
            result.add(jobSearchRespDTO);
        }
        return result;
    }
}
